package com.winning.sx.microframework.service.base.impl;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.dao.EasyDao;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.service.base.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private EasyDao dao;
    public DataTable queryList(Map<String, String> param) throws CustomException {
        try {
            String sql = "select c.id,c.passWarn,c.sysPass,c.type,c.source,c.messageLevel,c.createTime,c.updateTime,p.certCode,p.name,t.telephone,w.openId,w.name nickName" +
                    " from BASE_Customer c  LEFT OUTER JOIN BASE_PersonInfo p ON c.id=p.customerId and p.state='" + Constains.STATE_NORMAL+ "' LEFT OUTER JOIN" +
                    " BASE_Telephone t ON t.customerId=c.id  and t.state='" + Constains.STATE_NORMAL+ "' LEFT OUTER JOIN BASE_Wechat w ON w.customerId=c.id  and w.state='" + Constains.STATE_NORMAL+ "' LEFT OUTER JOIN" +
                    " BASE_Setting s ON s.customerId=c.id and s.state= '" + Constains.STATE_NORMAL+ "'  where c.createTime>=:b_createTime and c.endTime<=:e_createTime  order by c.id";
            return dao.querySql(sql,param);
        }catch (Exception e){
            throw new CustomException(1,e.getMessage());
        }
    }

    public DataTable queryType(Map<String, String> map) throws CustomException {
        String type = map.get("type").toString();
        try {
        if (type.equals("person")) {
            Map param=new HashMap();
            param.put("customerId",map.get("customerId"));
            return dao.queryList("BASE_PersonInfo",param);
        } else if (type.equals("telephone")) {
            Map param=new HashMap();
            param.put("customerId",map.get("customerId"));
            return dao.queryList("BASE_Telephone",param);
        } else if (type.equals("setting")) {
            Map param=new HashMap();
            param.put("customerId",map.get("customerId"));
            return dao.queryList("BASE_Setting",param);
        } else if (type.equals("wechat")) {
            Map param=new HashMap();
            param.put("customerId",map.get("customerId"));
            return dao.queryList("BASE_Wechat",param);
        } else if(type.equals("patient")){
            Map param=new HashMap();
            param.put("customerId",map.get("customerId"));
           String sql=" select i.* from BASE_PatientInfo i left outer join BASE_RELA_Customer_Patient p on i.id=p.patientId" +
                   " left outer join  BASE_Customer c on c.id=p.customerId " +
                   " where c.id=" + String.valueOf(map.get("customerId"));
            return dao.querySql(sql, param);
        } else {
            return null;
        }
        }catch (Exception e){
            throw new CustomException(1,e.getMessage());
        }
    }

    @Override
    public Boolean registerNewCustomer(Map<String, String> param) throws CustomException {
        if(param.get("openId")!=null){

            try {
                param.put("state", Constains.STATE_NORMAL);
                return dao.insert("PM_StaffWeChat",param);
            } catch (SQLException e) {
                e.printStackTrace();
                throw new CustomException(-2,e.getMessage());
            }

        }else{
            return false;
        }
    }


}
